package com.yuchting.dinneronline.server;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONArray;
import org.json.JSONObject;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Dinner {
	
	/**
	 * get the rank number to get the rank
	 * @author yuch
	 *
	 */
	public static class RankNumber{
		
		/**
		 * integer part
		 */
		public int integer;
		
		/**
		 * decimal digit part
		 */
		public int decimalDigit;
		
		public RankNumber(byte _rank){
			integer = _rank >>> 4;
			decimalDigit = _rank & 0x0f;
		}
		
		/**
		 * get the rank number as a float
		 * @return float type of rank
		 */
		public float asFloat(){
			float t_ret = integer;
			t_ret += (float)decimalDigit / 10;
			return t_ret;
		}
		
		public String toString(){
			return Float.toString(asFloat());
		}
	}
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private String m_name = "";
	
	@Persistent
	private double m_price = 9.5;
	
	@Persistent
	private int	m_orderNum = 0;
	
	// se xiang wei 
	@Persistent(serialized = "true") 
	private byte[]	m_rank		= {0,0,0};
	
	@Persistent(serialized = "true") 
	private long[] m_totalRank	= {0,0,0};
	
	@Persistent
	private long	m_commentNum = 0;
	
	public Dinner(){}
	
	public Dinner(String _name,double _price){
		m_name = _name;
		m_price = _price;
	}
	
	public String getName(){return m_name;}
	public double getPrice(){return m_price;}
	
	public int	getOrderNum(){return m_orderNum;}
	public void setOrderNum(int _num){m_orderNum = _num;}
	
	public byte[] getRank(){return m_rank;}
	public long[] getTotalRank(){return m_totalRank;}
	public long getCommnetNum(){return m_commentNum;}
	
	public RankNumber getRand(int _index)throws IllegalArgumentException{
		if(_index < 0 || _index > m_rank.length){
			throw new IllegalArgumentException("_index out of arr size:" + m_rank.length);
		}
		
		return new RankNumber(m_rank[_index]); 
	}
	
	/**
	 * cacluate the new rank record
	 * @param _newRank this is a new rank record from user
	 */
	public void calcCurrRank(byte[] _newRank){
		
		m_commentNum++;
		
		for(int i = 0;i < m_totalRank.length;i++){
			m_totalRank[i] += _newRank[i];
			
			float t_rank = (float)m_totalRank[i] / (float)m_commentNum;
			
			int t_hight = (int)Math.floor(t_rank);
			int t_low = (int)((t_rank - (float)t_hight) * 10);

			m_rank[i] = (byte)((t_hight << 4) | t_low);
		}
	}
		
	public JSONObject toJSON(){
		
		JSONObject t_result = new JSONObject();
		try{
			t_result.put("name",m_name);
			t_result.put("price",m_price);
			t_result.put("order_num",m_orderNum);
			t_result.put("commnet",m_commentNum);
					
			t_result.put("rank",getArrayJSON(m_rank));
			t_result.put("total_rank",getArrayJSON(m_totalRank));
			
			
		}catch(Exception e){
			System.err.println("Dinner toJSON Error:");
			e.printStackTrace(System.err);
		}
		
		return t_result;
	}
	
		
	
	public void fromJSON(JSONObject _json){
		
		try{
			m_name 		= _json.getString("name");
			m_price 	= _json.getDouble("price");
			m_orderNum	= _json.getInt("order_num");
			m_commentNum= _json.getLong("commnet");
			
			m_rank		= getByteArray("rank", _json);
			m_totalRank	= getLongArray("total_rank", _json);			
			
		}catch(Exception e){
			System.err.println("Dinner fromJSON Error:");
			e.printStackTrace(System.err);
		}
	}
	
	private static JSONArray getArrayJSON(byte[] _arr){
		JSONArray t_arrArray = new JSONArray();
		for(int c:_arr){
			t_arrArray.put(c);
		}
		return t_arrArray;
	}
	
	private static JSONArray getArrayJSON(long[] _arr){
		JSONArray t_arrArray = new JSONArray();
		for(long c:_arr){
			t_arrArray.put(c);
		}
		return t_arrArray;
	}
	
	private static byte[] getByteArray(String _name,JSONObject _json)throws Exception{
		JSONArray t_arr = _json.getJSONArray(_name);
		byte[] t_result = new byte[t_arr.length()];
		for(int i = 0;i < t_result.length;i++){
			t_result[i] = ((Integer)t_arr.get(i)).byteValue();
		}
		
		return t_result;
	}
	
	private static long[] getLongArray(String _name,JSONObject _json)throws Exception{
		JSONArray t_arr = _json.getJSONArray(_name);
		long[] t_result = new long[t_arr.length()];
		for(int i = 0;i < t_result.length;i++){
			t_result[i] = t_arr.getLong(i);
		}
		
		return t_result;
	}
	
	public static void main(String[] _arg){
		Dinner t_dinner = new Dinner();
		t_dinner.calcCurrRank(new byte[]{5,5,5});
		
		RankNumber t_num = t_dinner.getRand(0);
		
		t_dinner.calcCurrRank(new byte[]{3,2,4});
		t_num = t_dinner.getRand(0);
		
		System.out.print(t_num);
		
	}
}
